#include <stdio.h>
#include <string.h>
    
#include <termios.h>
#include <unistd.h>

#include <vga.h>
    
    
static struct termios configuration_initiale;
static struct termios configuration_brute;
    
static void initialisation_configuration_clavier();
static void initialisation_clavier();
static void restauration_clavier();
    
    
int getch()
{
  int c;

  /* on bascule */
  initialisation_clavier();  

  //c = getchar();
  c = vga_getkey();
    
  /* retour au mode normal */
  restauration_clavier();

  return c;
}
    

/* Gestion du clavier en mode brut */
    
    
static void initialisation_configuration_clavier()
{
  static char is_init=0;   
        
  if(!is_init)
    {
      is_init = 1;
        
      tcgetattr(STDIN_FILENO, &configuration_initiale);
        
      configuration_brute = configuration_initiale;

      cfmakeraw(&configuration_brute);
      configuration_brute.c_cc[VMIN]  = 1;
      configuration_brute.c_cc[VTIME] = 0;
    }
}
    
static void initialisation_clavier()
{
  initialisation_configuration_clavier();   
  tcsetattr(STDIN_FILENO, TCSANOW, &configuration_brute);
}
      
static void restauration_clavier()
{
  tcsetattr(STDIN_FILENO, TCSANOW, &configuration_initiale);
}
